package sjsu.android;
import dbAdapter.FoodDBAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener ;

public class NutritionInfo extends Activity {
    /** Called when the activity is first created. */
	static TextView str;
	FoodDBAdapter fd;
	String NutritionList[] = new String [16];
	String[] itemList,nutritionInfoList;
	Cursor cursor;
	final static String[] Nutrients ={ "Quantity:", "Calories:", "Fat:",
 			 "Saturated Fat:", "Cholestrol:", "Sodium:","Carbohydrate:","Fiber:","Sugar:","Protein:", "Vit A:", "Vit B:","Vit C:","Calcium:","Iron:" };
	final static String[] qty = new String[17];
	private static class EfficientAdapter extends BaseAdapter {
			 private LayoutInflater mInflater;
			 ViewHolder holder;
			
		//	 Text str = (TextView)findViewById(R.id.TextView02);
			 public EfficientAdapter(Context context) {
			 mInflater = LayoutInflater.from(context);
			 
			 }
	
			 
			 public int getCount() {
			 return Nutrients.length;
			 }
			 
			 public Object getItem(int position) {
			 return position;
			 }
			 
			 public long getItemId(int position) {
			 return position;
			 }
			 
		//	 @Override
			 public View getView(int position, View convertView, ViewGroup parent) {
		//	 ViewHolder holder;
			 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.nutrition, null);
			 holder = new ViewHolder();
			 
			 holder.text2 = (TextView) convertView
			 .findViewById(R.id.TextView02);
			 holder.text3 = (TextView) convertView
			 .findViewById(R.id.TextView03);


			 convertView.setTag(holder);
			 } else {
			 holder = (ViewHolder) convertView.getTag();
				}
			 
			 holder.text2.setText(Nutrients[position]);
			 holder.text3.setText(qty[position]);
			 return convertView;
			 }
	
			 
			 static class ViewHolder {
			 TextView text1,text2,text3;
			
			
			 }
			 }
			 
			 @Override
			 public void onCreate(Bundle savedInstanceState) {
			 super.onCreate(savedInstanceState);
			 Intent myIntent = getIntent(); // this is just for example purpose
			 String item = myIntent.getStringExtra("itemName");
			 System.out.println("intent get extra: "+ item);
			 fd = new FoodDBAdapter(this);
			 fd.open();
       		cursor = fd.fetchNutrition(item);
       		nutritionInfoList =  new String[cursor.getCount()];
       		while( cursor.moveToNext()){
       		
       		for (int i = 0, j=3; i<15;i++,j++){
    			NutritionList[i] = cursor.getString(j);
    			System.out.println(cursor.getString(j));
    			qty[i] = NutritionList[i];
    		//	text1.setText(NutritionList[0]);
    			
       		}}
 

      		fd.close(); 

			 setContentView(R.layout.nutritionlist);
			 ListView l1 = (ListView) findViewById(R.id.ListView01);
			 l1.setAdapter(new EfficientAdapter(this));
			 
			  str = (TextView)findViewById(R.id.TextView03);
			  str.setText(item);
	//		  l1.setOnItemClickListener(new OnItemClickListener() {
				  
				   
				  
		
}
			  
			 
	//		 private static final String[] Nutrients = { "Quantity:", "Calories:", "Fat:",
	//		 "Saturated Fat:", "Cholestrol:", "Sodium:","Carbohydrate:","Fiber:","Sugar:","Protein:" };
	//		 private static final String[] qty = { "1 cup", "300", "1.5 g", "0.2 g", "0 mg",
	//		 "2 mg", "61 g", "2 g", "0 g", "11 g" };
			 	 
			
	 
		
 
}